package com.zhiyou100.loans.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.zhiyou100.loans.dao.UserDao;
import com.zhiyou100.loans.dao.impl.UserDaoImpl;
import com.zhiyou100.loans.model.Balance;
import com.zhiyou100.loans.model.Result;
import com.zhiyou100.loans.model.User;
import com.zhiyou100.loans.service.UserService;
import com.zhiyou100.loans.util.NetDate;

import net.sf.json.JSONObject;

public class UserServiceImpl implements UserService {
	UserDao ud = new UserDaoImpl();
	
	
	@Override
	public JSONObject add(User user) {
		user.setRegistertime(new Date());
		Result result = new Result();
		int i = ud.insert(user);
		if(i>0){
			result.setFlag(true);
			result.setMessage("注册成功");
		}else{
			result.setFlag(false);
			result.setMessage("注册失败");
		}
		return JSONObject.fromObject(result);
	}


	@Override
	public JSONObject login(User user,HttpServletRequest request) {
		ResultSet rs = ud.select(user);
		Result result = new Result();
		Date date = NetDate.getCurrentTime();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int days = cal.get(Calendar.DATE);
		try{
			if(rs.next()){
				user.setAddr(rs.getString("addr"));
				user.setEmail(rs.getString("email"));
				user.setIdnumber(rs.getString("idnumber"));
				user.setPassword(rs.getString("password"));
				user.setPhonenumber(rs.getString("phonenumber"));
				user.setRealname(rs.getString("realname"));
				user.setRegistertime(rs.getDate("registertime"));
				user.setUsername(rs.getString("username"));
				user.setId(rs.getInt("id"));
				user.setCurrentdate(days);
				//把user对象共享到会话中
				//html不能使用el和jstl表达式，能够使用json对象
				request.getSession().setAttribute("user", user);
				
				
				result.setFlag(true);
				result.setMessage("登录成功！");
			
			}else{//账号和密码不匹配
				result.setFlag(false);
				result.setMessage("账号或者密码错误");
			}
		}catch(SQLException e){
			result.setFlag(false);
			result.setMessage("登录失败，请重新登录");
			e.printStackTrace();
		}
		
		
		return JSONObject.fromObject(result);
	}


	@Override
	public JSONObject logout(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		
		Result result = new Result();
		result.setFlag(true);
		result.setMessage("退出成功");
		return JSONObject.fromObject(result);
	}


	@Override
	public JSONObject balanceSelect(String userid) {
		ResultSet rs = ud.balanceSelect(Integer.parseInt(userid));
		Balance balance = null;
		try {
			if(rs.next()){
				balance = new Balance();
				balance.setId(rs.getInt("id"));
				balance.setMoney(rs.getDouble("money"));
				balance.setUserid(rs.getInt("userid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return JSONObject.fromObject(balance);
	}
	
	
	public synchronized int createBalance(String username){
		ResultSet rs = ud.select(username);
		int id=0;
		try {
			if(rs.next()){
				id = rs.getInt("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i=ud.balanceInsert(id);
		return i;
	}


	@Override
	public JSONObject balanceInsert(String username) {
		int i=createBalance(username);
		Result result = new Result(false, "用户余额创建失败");
		if(i>0){
			result.setFlag(true);
			result.setMessage("用户余额创建成功");
		}
		return JSONObject.fromObject(result);
	}


	@Override
	public JSONObject updateBalance(String money,String userid) {
		int i=ud.updateBalance(Double.parseDouble(money),Integer.parseInt(userid));
		Result result = new Result(false, "充值失败");
		if(i>0){
			result.setFlag(true);
			result.setMessage("充值成功");
		}
		//如果有对象包含日期属性，需要使用自定义的日期处理器对该属性进行设置
		return JSONObject.fromObject(result);
	}


	@Override
	public JSONObject minusBalance(String money, String userid) {
		//查询数据库月是否充足
		double balance = getBalanceByUserid(userid);
		Result result = new Result(false, "失败");
		if(balance>=Double.parseDouble(money)){
			int i=ud.updateBalance(0-Double.parseDouble(money),Integer.parseInt(userid));
			if(i>0){
				result.setFlag(true);
				result.setMessage("成功");
			}
		}
		return JSONObject.fromObject(result);
	}
	
	public double getBalanceByUserid(String userid){
		ResultSet rs=ud.selectBalanceByUserid(Integer.parseInt(userid));
		double balance=0;
		try {
			if(rs.next()){
				balance = rs.getDouble(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return balance;
	}

}
